<#import "/manage/tpl/pageTep.ftl" as page>
<@page.pageBase currentMenu="商品促销">
<form id="searchForm" class="form-panel">
    <ul class="panel-content">
        <li>
            <div class="control-group span10">
                <label class="control-label">活动ID：</label>
                <div class="controls">
                    <input type="text" class="input-small search-query" name="id"  id="id"/>
                </div>
            </div>
            <div class="control-group span7">
                <label class="control-label">活动类型：</label>
                <div class="controls">
                    <#assign map = {'':'全部','c':'促销活动','j':'积分兑换','t':'团购活动'}>
                    <select name="activityType" class="input-normal"  id="activityType" >
                        <#list map?keys as key>
                            <option value="${key}">${map[key]}</option>
                        </#list>
                    </select>
                </div>
            </div>
            <div class="control-group span7">
                <label class="control-label">优惠方式：</label>
                <div class="controls">
                    <#assign map = {'':'','r':'减免','d':'折扣','s':'双倍积分'}>
                    <select name="discountType" class="input-normal" id="discountType" data-rule="优惠方式:required;discountType;">
                        <#list map?keys as key>
                            <option value="${key}">${map[key]}</option>
                        </#list>
                    </select>
                </div>
            </div>
        </li>
        <li>
            <div class="control-group span8">
                <label class="control-label">状态：</label>
                <div class="controls">
                    <#assign map = {'':'','y':'显示','n':'不显示'}>
                    <select name="status" class="input-normal" id="status">
                        <#list map?keys as key>
                            <option value="${key}">${map[key]}</option>
                        </#list>
                    </select>
                </div>
            </div>
            <div class="form-actions span8">
                <button  type="submit" class="button button-primary"  >
                    <i class="icon-search icon-white"></i> 查询
                </button>
                <a href="javascript:add()" class="button button-success"><i class="icon-plus-sign icon-white"></i> 添加</a>
                <button  class="button button-danger"  onclick="return delFunction()">
                    <i class="icon-remove-sign icon-white"></i>删除
                </button>
            </div>
        </li>
    </ul>
</form>
<div id="grid"></div>


<div id="addContent"  style="display:none;">
<form id="addForm" class="form-horizontal" action="${basepath}/manage/activity/insertJson" method="post">
    <div class="row">
        <div class="control-group span16">
            <label class="control-label">'活动ID：</label>
            <div class="controls">
                <input type="text" class="input-normal control-text" name="id" data-rules="{required : true}">
            </div>
        </div>
        <div class="control-group span16">
            <label class="control-label">活动名称：</label>
            <div class="controls">
                <input type="text" class="input-normal control-text" name="name" data-rules="{required : true}">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="control-group span16">
            <label class="control-label">活动类型：</label>
            <div class="controls">
                <input type="text" class="input-normal control-text" name="'activityType" data-rules="{required : true}">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="control-group span16">
            <label class="control-label">优惠方式：</label>
            <div class="controls">
                <input type="text" class="input-normal control-text" name="'discountType" data-rules="{required : true}">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="control-group span16">
            <label class="control-label">活动明细：</label>
            <div class="controls">
                <input type="text" class="input-normal control-text" name="'" data-rules="{required : true}">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="control-group span16">
            <label class="control-label">状态：</label>
            <div class="controls">
                <input type="text" class="input-normal control-text" name="status'" data-rules="{required : true}">
            </div>
        </div>
    </div>
</form>
</div>

<div id="editContent"  style="display:none;">
    <form id="editForm" class="form-horizontal" action="${basepath}/manage/activity/updateJson" method="post">
        <input type="hidden" class="input-normal control-text" name="id" />
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">'活动ID：</label>
                <div class="controls">
                     <input type="text" class="input-normal control-text" name="id" data-rules="{required : true}">
                 </div>
            </div>
            <div class="control-group span16">
                <label class="control-label">活动名称：</label>
                <div class="controls">
                     <input type="text" class="input-normal control-text" name="name" data-rules="{required : true}">
                </div>
             </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">活动类型：</label>
                <div class="controls">
                    <input type="text" class="input-normal control-text" name="'activityType" data-rules="{required : true}">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">优惠方式：</label>
                <div class="controls">
                    <input type="text" class="input-normal control-text" name="'discountType" data-rules="{required : true}">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">活动明细：</label>
                <div class="controls">
                    <input type="text" class="input-normal control-text" name="'" data-rules="{required : true}">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">状态：</label>
                <div class="controls">
                    <input type="text" class="input-normal control-text" name="status'" data-rules="{required : true}">
                </div>
            </div>
        </div>
    </form>
</div>



<script>
    var Grid = BUI.Grid,
            Store = BUI.Data.Store,
            columns = [
                {title : '活动ID',dataIndex :'id', width:100},
                {title : '活动名称',dataIndex :'name', width:100},
                {title : '活动类型',dataIndex : 'activityType',width:100, renderer:function(data){
                    if(data == "c"){
                        return '<span class="badge badge-info">促销活动</span>';
                    };
					if(data == "j"){
                        return '<span class="badge badge-success">积分兑换</span>';
                    };
                    if(data == "t"){
                        return '<span class="badge">团购活动</span>';
                    };
                    return '';

                }},
                {title : '优惠方式',dataIndex : 'discountType',width:80,renderer:function(data){
                    if(data == "r"){
                        return '减免';
                    };
                    if(data == "d"){
                        return '折扣';
                    };
                    if(data == "s"){
                        return '双倍积分';
                    };
                    return '异常';

                }},
                {title : '活动明细',dataIndex : '',width:450, renderer:function(data,obj,index){

                    var html = '活动时间：'+obj.startDate+'~'+obj.endDate;
                    console.log(obj.expire);
                    if(!obj.expire){
                        html+='<span class="label label-danger" style="background-color:Red;">活动已到期</span>';
                    }
                    html+='<br>商品ID:'+obj.productID+'<br>';
                    if(obj.exchangeScore!=0){
                        html+='兑换积分:'+obj.exchangeScore+'<br>';
                    }
                    if(obj.activityType=='t'){
                        html+='最低团购人数:'+obj.minGroupCount+'<br>';
                        html+='团购价:'+obj.tuanPrice+'<br>';
                    }
                    return html;


                }},
                {title : '状态',dataIndex : 'status',width:50, renderer:function(data){
                    if(data == "y"){
                        return '<img alt="显示" src="${basepath}/resource/images/action_check.gif">';
                    } else {
                        return '<img alt="不显示" src="${basepath}/resource/images/action_delete.gif">';
                    }
                }},
                {title : '操作',dataIndex : 'id',width:50,renderer : function (value) {
                    return '<a href="javascript:edit(' +value+ ')">编辑</a>';

                }}
            ];

    var store = new Store({
                url : 'loadData',
                autoLoad:true, //自动加载数据
                params : { //配置初始请求的参数
                    length : '10',
                    id:$("#id").val(),
                    activityType:$("#activityType").val(),
                    discountType:$("#discountType").val(),
                    status:$("#status").val()
                },
                pageSize:10,	// 配置分页数目
                root : 'list',
                totalProperty : 'total'
            });
	var grid = new Grid.Grid({
                render:'#grid',
                columns : columns,
                loadMask: true, //加载数据时显示屏蔽层
                store: store,
                plugins : [Grid.Plugins.CheckSelection], // 插件形式引入多选表格

                // 底部工具栏
                bbar:{
                    pagingBar:true
                }
            });
    grid.on("aftershow", function (e) {
        //万恶的json导致菜单栏高度问题
        $(".dl-second-nav").css("height", $(".dl-inner-tab").css("height"));
    });
    grid.render();
    var form = new BUI.Form.HForm({
        srcNode : '#searchForm'
    }).render();

    form.on('beforesubmit',function(ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });

    //删除选中的记录
    function delFunction(){
        var selections = grid.getSelection();
        var ids=new Array();
        for(var i=0;i<selections.length;i++){
            ids[i]=selections[i].id.toString()
        }
        $.ajax({
            type: "POST",
            url: "${basepath}/manage/activity/deletesJson",
            dataType: "json",
            data: {
                ids:ids
            },
            success: function (data) {
                var obj = form.serializeToObject();
                obj.start = 0; //返回第一页
                store.load(obj);
            }
        });

    }
    /*----------------------------------------------新增表单处理 begin------------------------------------*/
    var addForm = new BUI.Form.Form({
        srcNode: '#addForm',
        submitType : 'ajax',
        callback : function(data){
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            addDialog.close();
        }
    }).render();

    var addDialog = new BUI.Overlay.Dialog({
        title: '新增商品促销',
        width: 500,
        height: 400,
        contentId: 'addContent',
        success: function () {
            addForm.ajaxSubmit();
        }
    });
    //添加按钮事件
    function add() {
        addDialog.show();
    }
    /*----------------------------------------------新增表单处理 end------------------------------------*/

    /*----------------------------------------------编辑表单处理 begin------------------------------------*/
    var editForm = new BUI.Form.Form({
        srcNode: '#editForm',
        submitType : 'ajax',
        callback : function(data){
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            editDialog.close();
        }
    });
    editForm.render();

    var editDialog = new BUI.Overlay.Dialog({
        title: '编辑商品促销',
        width: 500,
        height: 400,
        contentId: 'editContent',
        success: function () {
            editForm.ajaxSubmit();
        }
    });
    //编辑按钮事件
    function edit(id) {
        $.ajax({
            type:"POST",
            dataType:"json",
            url:"${basepath}/manage/activity/toEditJson",
            data:{id:id},
            success:function(data) {
                var form =$("#editForm")[0];
                BUI.FormHelper.setFields(form,data.data);
                editDialog.show();
            }
        });
    }
    /*----------------------------------------------编辑表单处理 end------------------------------------*/


</script>


</@page.pageBase>